import {getBusinessUserRevenue} from "../../ajax/module/userInfo.js"
import imgUrl from "../../images/util.js"

var app = getApp();
Page({
    data:{
        date1: '2018-01-01',
        date2: '2021-01-15',
        selectRes:{},
        calendarIcon:imgUrl.calendarIcon,
        fixedTime:[
            {
                name:"今天",
                id:"1",
                selected:true,
              
            },
            {
                name:"昨天",
                id:"2" ,
                selected:false
            },
            {
                name:"本周",
                id:"3" ,
                selected:false
            },
            {
                name:"本月",
                id:"4" ,
                selected:false
            }

        ]
    },
    
    onLoad(){
      this.handle(1)
    },
    DateChange1(e) {
        this.setData({
          date1: e.detail.value
        })
        this.select()
    },
    DateChange2(e) {
        this.setData({
          date2: e.detail.value
        })
        this.select()


    },
    selectBy(e){
        // console.log()
        let {id}=e.currentTarget.dataset.item
        // e.currentTarget.dataset.item.selected=true
        let data=JSON.parse(JSON.stringify(this.data.fixedTime)) 
    
        data.forEach(item=>{
            item.selected=false
            if(item.id == id){
                item.selected=true
            }
        })

        console.log(data)
        this.setData({
            fixedTime:data
        })
        this.handle(id)

    },
    handle(id){
        if(id == 1){
          this.setData({
            date1:this.getNowFormatDate() ,
            date2:this.getNowFormatDate() 
          })
          this.select()
        }

        if(id == 2) {
            this.setData({
                date1:this.getYesterdayDate() ,
                date2:this.getYesterdayDate() 
              })
              this.select()
        }
        if(id == 3){
            // console.log(this.getFirstDayOfWeek())

            this.setData({
                date1:this.getFirstDayOfWeek() ,
                date2:this.getNowFormatDate() 
            })
            this.select()
        }
        if(id == 4){
            this.setData({
                date1:this.getFirstDayOfMonth() ,
                date2:this.getNowFormatDate() 
            })
            this.select()
        }

    },

    select(){
        let begin=this.data.date1+""
        let end=this.data.date2+""
        if(new Date(begin).getTime()>new Date(end).getTime()){
            wx.showToast({
                icon:"none",
                title:"起止时间有误",
                duration:2000
            })
        }else{
            
            getBusinessUserRevenue({begin,end}).then(res=>{
                let {
                    refundOrderNum,
                    totalOrderNum,
                    totalTurnover,
                    unitPrice
                }=res.data
                this.setData({
                    selectRes:{
                        refundOrderNum,
                        totalOrderNum,
                        totalTurnover,
                        unitPrice
                    }
                })
    
            })
        }

       
    },
    // 获取今天时间
    getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
          month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
          strDate = "0" + strDate;
        }
        var currentdate =  year + seperator1 + month + seperator1 + strDate +"";
        return currentdate;
    },
    // 获取昨天的时间
    getYesterdayDate(){
        var time=(new Date).getTime()-24*60*60*1000;
        var yesterday=new Date(time);
        var month=yesterday.getMonth();
        var day=yesterday.getDate();
        yesterday=yesterday.getFullYear() + "-" + (yesterday.getMonth()> 9 ? (yesterday.getMonth() + 1) : "0"
         + (yesterday.getMonth() + 1)) + "-" +(yesterday.getDate()> 9 ? (yesterday.getDate()) : "0"
         + (yesterday.getDate()));
      return yesterday
    },
    // 获取本周的时间
    getFirstDayOfWeek(){
        function timeFormat(date) {
               if (!date || typeof(date) === "string") {
                   this.error("参数异常，请检查...");
               }
               var y = date.getFullYear(); //年
              var m =  (date.getMonth()> 9 ? (date.getMonth() + 1) : "0"
              + (date.getMonth() + 1)); //月
              var d = (date.getDate()> 9 ? (date.getDate()) : "0"
              + (date.getDate())); //日
            
                return y + "-" + m + "-" + d;
            }


        var date=new Date()
        var weekday = date.getDay()||7; //获取星期几,getDay()返回值是 0（周日） 到 6（周六） 之间的一个整数。0||7为7，即weekday的值为1-7
         
       date.setDate(date.getDate()-weekday+1);//往前算（weekday-1）天，年份、月份会自动变化
       return timeFormat(date);
    },
    //获取本月的第一天
    getFirstDayOfMonth (date) {
        function timeFormat(date) {
            if (!date || typeof(date) === "string") {
                this.error("参数异常，请检查...");
            }
            var y = date.getFullYear(); //年
           var m =  (date.getMonth()> 9 ? (date.getMonth() + 1) : "0"
           + (date.getMonth() + 1)); //月
           var d = (date.getDate()> 9 ? (date.getDate()) : "0"
           + (date.getDate())); //日
         
             return y + "-" + m + "-" + d;
         }
         var date=new Date()
             date.setDate(1);
           return timeFormat(date);
     }


})